package clientGUI.studentGUI;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;

import javax.swing.*;

import clientApp.VirtualAdvisor;
import clientSocket.RequestInterpreter;


public class GUI_Function_Library {

	// ---- DEAL WITH EXCEPTIONS
		public static void exceptionHandler(String additionalMessage, Exception e)
		{
			JOptionPane.showMessageDialog(null, "CORRECT GUI EXCEPTION HANDLER: " + additionalMessage + ": " + e.getMessage());
			e.printStackTrace();
		}
		public static void exceptionHandler(Exception e)
		{
			exceptionHandler("", e);
		}

	// ---- FRAME EXECUTION	
		/*
		 * close frame
		 */
		public static void undisplayFrame(JFrame frame)
		{
//			frame.setVisible(false);
			frame.dispose();
		}
		
		/*
		 * display the pane
		 * exit on close - exit out of program if the frame is exited out of
		 */
		public static JFrame displayPane (String title, JPanel pane, boolean exitOnClose)
		{
			final JFrame frame = new JFrame(title);
			frame.add(pane);
			frame.pack();
			frame.setVisible(true);
			
			if(exitOnClose)
			{
//				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
				
				frame.addWindowListener (new WindowAdapter()
				{
					public void windowClosing(WindowEvent e)
					{
						// confirm and exit
						int closeNow = JOptionPane.showConfirmDialog(frame, "Exit Now? This will terminate the program.", "Exit Now?", JOptionPane.YES_NO_OPTION);
						if(closeNow == JOptionPane.YES_OPTION) {
							try {
								VirtualAdvisor.saveData();
								RequestInterpreter.terminate();
							} catch (IOException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (ClassNotFoundException e2) {
								// TODO Auto-generated catch block
								e2.printStackTrace();
							}
							System.exit(0);
						}
					}
				});
				
			}
			else
				frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
			
			return frame;
		}
}
